using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using SheLife.Domain.Interfaces;

namespace SheLife.Application.Services
{
    /// <summary>
    /// 膳食记录服务实现
    /// </summary>
    public class DietRecordService : IDietRecordService
    {
        private readonly IUnitOfWork _unitOfWork;

        public DietRecordService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取膳食记录
        /// </summary>
        /// <param name="id">膳食记录ID</param>
        /// <returns>膳食记录实体</returns>
        public async Task<DietRecord?> GetDietRecordByIdAsync(Guid id)
        {
            return await _unitOfWork.DietRecordRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据用户ID获取膳食记录列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>膳食记录列表</returns>
        public async Task<IEnumerable<DietRecord>> GetDietRecordsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.DietRecordRepository.GetByUserIdAsync(userId);
        }

        /// <summary>
        /// 根据用户ID和日期范围获取膳食记录列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>膳食记录列表</returns>
        public async Task<IEnumerable<DietRecord>> GetDietRecordsByUserIdAndDateRangeAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            return await _unitOfWork.DietRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);
        }

        /// <summary>
        /// 根据用户ID和餐次类型获取膳食记录列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="mealType">餐次类型</param>
        /// <returns>膳食记录列表</returns>
        public async Task<IEnumerable<DietRecord>> GetDietRecordsByUserIdAndMealTypeAsync(Guid userId, string mealType)
        {
            return await _unitOfWork.DietRecordRepository.GetByUserIdAndMealTypeAsync(userId, mealType);
        }

        /// <summary>
        /// 创建膳食记录
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="date">日期</param>
        /// <param name="mealType">餐次类型</param>
        /// <param name="description">描述</param>
        /// <param name="calories">卡路里</param>
        /// <param name="notes">备注</param>
        /// <returns>膳食记录实体</returns>
        public async Task<DietRecord> CreateDietRecordAsync(Guid userId, DateTime date, string mealType, string description, decimal calories, string? notes = null)
        {
            var dietRecord = DietRecord.Create(userId, date, mealType, description, calories, notes);
            await _unitOfWork.DietRecordRepository.AddAsync(dietRecord);
            await _unitOfWork.SaveChangesAsync();
            return dietRecord;
        }

        /// <summary>
        /// 更新膳食记录
        /// </summary>
        /// <param name="id">膳食记录ID</param>
        /// <param name="date">日期</param>
        /// <param name="mealType">餐次类型</param>
        /// <param name="description">描述</param>
        /// <param name="calories">卡路里</param>
        /// <param name="notes">备注</param>
        /// <returns>膳食记录实体</returns>
        public async Task<DietRecord?> UpdateDietRecordAsync(Guid id, DateTime? date, string? mealType, string? description, decimal? calories, string? notes = null)
        {
            var dietRecord = await _unitOfWork.DietRecordRepository.GetByIdAsync(id);
            if (dietRecord == null)
            {
                return null;
            }

            dietRecord.Update(date, mealType, description, calories, notes);
            await _unitOfWork.DietRecordRepository.UpdateAsync(dietRecord);
            await _unitOfWork.SaveChangesAsync();
            return dietRecord;
        }

        /// <summary>
        /// 删除膳食记录
        /// </summary>
        /// <param name="id">膳食记录ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteDietRecordAsync(Guid id)
        {
            var dietRecord = await _unitOfWork.DietRecordRepository.GetByIdAsync(id);
            if (dietRecord == null)
            {
                return false;
            }

            await _unitOfWork.DietRecordRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 获取膳食统计数据
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="days">统计天数</param>
        /// <returns>统计数据字典</returns>
        public async Task<Dictionary<string, decimal>> GetDietStatisticsAsync(Guid userId, int days = 7)
        {
            var endDate = DateTime.Today;
            var startDate = endDate.AddDays(-days);
            var dietRecords = await _unitOfWork.DietRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);

            var statistics = new Dictionary<string, decimal>();

            if (dietRecords.Any())
            {
                // 总体统计
                statistics["TotalCalories"] = dietRecords.Sum(d => d.Calories);
                statistics["AverageCalories"] = dietRecords.Average(d => d.Calories);
                statistics["TotalRecords"] = dietRecords.Count();

                // 按餐次类型统计
                var recordsByMealType = dietRecords.GroupBy(d => d.MealType);
                foreach (var group in recordsByMealType)
                {
                    statistics[$"{group.Key}_TotalCalories"] = group.Sum(d => d.Calories);
                    statistics[$"{group.Key}_AverageCalories"] = group.Average(d => d.Calories);
                    statistics[$"{group.Key}_Count"] = group.Count();
                }
            }
            else
            {
                statistics["TotalCalories"] = 0;
                statistics["AverageCalories"] = 0;
                statistics["TotalRecords"] = 0;
            }

            return statistics;
        }
    }

    /// <summary>
    /// 膳食营养服务实现
    /// </summary>
    public class DietNutrientService : IDietNutrientService
    {
        private readonly IUnitOfWork _unitOfWork;

        public DietNutrientService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取膳食营养
        /// </summary>
        /// <param name="id">膳食营养ID</param>
        /// <returns>膳食营养实体</returns>
        public async Task<DietNutrient?> GetDietNutrientByIdAsync(Guid id)
        {
            return await _unitOfWork.DietNutrientRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据膳食记录ID获取膳食营养列表
        /// </summary>
        /// <param name="dietRecordId">膳食记录ID</param>
        /// <returns>膳食营养列表</returns>
        public async Task<IEnumerable<DietNutrient>> GetDietNutrientsByDietRecordIdAsync(Guid dietRecordId)
        {
            return await _unitOfWork.DietNutrientRepository.GetByDietRecordIdAsync(dietRecordId);
        }

        /// <summary>
        /// 根据用户ID和日期范围获取膳食营养列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>膳食营养列表</returns>
        public async Task<IEnumerable<DietNutrient>> GetDietNutrientsByUserIdAndDateRangeAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            return await _unitOfWork.DietNutrientRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);
        }

        /// <summary>
        /// 创建膳食营养
        /// </summary>
        /// <param name="dietRecordId">膳食记录ID</param>
        /// <param name="nutrientName">营养名称</param>
        /// <param name="amount">数量</param>
        /// <param name="unit">单位</param>
        /// <returns>膳食营养实体</returns>
        public async Task<DietNutrient> CreateDietNutrientAsync(Guid dietRecordId, string nutrientName, decimal amount, string unit)
        {
            var dietNutrient = DietNutrient.Create(dietRecordId, nutrientName, amount, unit);
            await _unitOfWork.DietNutrientRepository.AddAsync(dietNutrient);
            await _unitOfWork.SaveChangesAsync();
            return dietNutrient;
        }

        /// <summary>
        /// 更新膳食营养
        /// </summary>
        /// <param name="id">膳食营养ID</param>
        /// <param name="nutrientName">营养名称</param>
        /// <param name="amount">数量</param>
        /// <param name="unit">单位</param>
        /// <returns>膳食营养实体</returns>
        public async Task<DietNutrient?> UpdateDietNutrientAsync(Guid id, string? nutrientName, decimal? amount, string? unit)
        {
            var dietNutrient = await _unitOfWork.DietNutrientRepository.GetByIdAsync(id);
            if (dietNutrient == null)
            {
                return null;
            }

            dietNutrient.Update(nutrientName, amount, unit);
            await _unitOfWork.DietNutrientRepository.UpdateAsync(dietNutrient);
            await _unitOfWork.SaveChangesAsync();
            return dietNutrient;
        }

        /// <summary>
        /// 删除膳食营养
        /// </summary>
        /// <param name="id">膳食营养ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteDietNutrientAsync(Guid id)
        {
            var dietNutrient = await _unitOfWork.DietNutrientRepository.GetByIdAsync(id);
            if (dietNutrient == null)
            {
                return false;
            }

            await _unitOfWork.DietNutrientRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 获取营养统计数据
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="days">统计天数</param>
        /// <returns>统计数据字典</returns>
        public async Task<Dictionary<string, decimal>> GetNutrientStatisticsAsync(Guid userId, int days = 7)
        {
            var endDate = DateTime.Today;
            var startDate = endDate.AddDays(-days);
            var nutrients = await _unitOfWork.DietNutrientRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);

            var statistics = new Dictionary<string, decimal>();

            if (nutrients.Any())
            {
                // 按营养名称分组统计
                var nutrientsByName = nutrients.GroupBy(n => n.NutrientName);
                foreach (var group in nutrientsByName)
                {
                    statistics[$"{group.Key}_TotalAmount"] = group.Sum(n => n.Amount);
                    statistics[$"{group.Key}_AverageAmount"] = group.Average(n => n.Amount);
                }
            }

            return statistics;
        }
    }

    /// <summary>
    /// 食谱服务实现
    /// </summary>
    public class RecipeService : IRecipeService
    {
        private readonly IUnitOfWork _unitOfWork;

        public RecipeService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取食谱
        /// </summary>
        /// <param name="id">食谱ID</param>
        /// <returns>食谱实体</returns>
        public async Task<Recipe?> GetRecipeByIdAsync(Guid id)
        {
            return await _unitOfWork.RecipeRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 获取所有食谱
        /// </summary>
        /// <returns>食谱列表</returns>
        public async Task<IEnumerable<Recipe>> GetAllRecipesAsync()
        {
            return await _unitOfWork.RecipeRepository.GetAllAsync();
        }

        /// <summary>
        /// 根据分类获取食谱列表
        /// </summary>
        /// <param name="category">分类</param>
        /// <returns>食谱列表</returns>
        public async Task<IEnumerable<Recipe>> GetRecipesByCategoryAsync(string category)
        {
            return await _unitOfWork.RecipeRepository.GetByCategoryAsync(category);
        }

        /// <summary>
        /// 根据标签获取食谱列表
        /// </summary>
        /// <param name="tag">标签</param>
        /// <returns>食谱列表</returns>
        public async Task<IEnumerable<Recipe>> GetRecipesByTagAsync(string tag)
        {
            return await _unitOfWork.RecipeRepository.GetByTagAsync(tag);
        }

        /// <summary>
        /// 搜索食谱
        /// </summary>
        /// <param name="keyword">关键词</param>
        /// <returns>食谱列表</returns>
        public async Task<IEnumerable<Recipe>> SearchRecipesAsync(string keyword)
        {
            return await _unitOfWork.RecipeRepository.SearchByNameOrDescriptionAsync(keyword);
        }

        /// <summary>
        /// 创建食谱
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="description">描述</param>
        /// <param name="category">分类</param>
        /// <param name="tags">标签列表</param>
        /// <param name="prepTime">准备时间</param>
        /// <param name="cookTime">烹饪时间</param>
        /// <param name="servings">份数</param>
        /// <param name="calories">卡路里</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>食谱实体</returns>
        public async Task<Recipe> CreateRecipeAsync(string name, string description, string category, List<string> tags, int prepTime, int cookTime, int servings, decimal calories, string? imageUrl = null)
        {
            var recipe = Recipe.Create(name, description, category, tags, prepTime, cookTime, servings, calories, imageUrl);
            await _unitOfWork.RecipeRepository.AddAsync(recipe);
            await _unitOfWork.SaveChangesAsync();
            return recipe;
        }

        /// <summary>
        /// 更新食谱
        /// </summary>
        /// <param name="id">食谱ID</param>
        /// <param name="name">名称</param>
        /// <param name="description">描述</param>
        /// <param name="category">分类</param>
        /// <param name="tags">标签列表</param>
        /// <param name="prepTime">准备时间</param>
        /// <param name="cookTime">烹饪时间</param>
        /// <param name="servings">份数</param>
        /// <param name="calories">卡路里</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>食谱实体</returns>
        public async Task<Recipe?> UpdateRecipeAsync(Guid id, string? name, string? description, string? category, List<string>? tags, int? prepTime, int? cookTime, int? servings, decimal? calories, string? imageUrl = null)
        {
            var recipe = await _unitOfWork.RecipeRepository.GetByIdAsync(id);
            if (recipe == null)
            {
                return null;
            }

            recipe.Update(name, description, category, tags, prepTime, cookTime, servings, calories, imageUrl);
            await _unitOfWork.RecipeRepository.UpdateAsync(recipe);
            await _unitOfWork.SaveChangesAsync();
            return recipe;
        }

        /// <summary>
        /// 删除食谱
        /// </summary>
        /// <param name="id">食谱ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteRecipeAsync(Guid id)
        {
            var recipe = await _unitOfWork.RecipeRepository.GetByIdAsync(id);
            if (recipe == null)
            {
                return false;
            }

            await _unitOfWork.RecipeRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }

    /// <summary>
    /// 食谱食材服务实现
    /// </summary>
    public class RecipeIngredientService : IRecipeIngredientService
    {
        private readonly IUnitOfWork _unitOfWork;

        public RecipeIngredientService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取食谱食材
        /// </summary>
        /// <param name="id">食谱食材ID</param>
        /// <returns>食谱食材实体</returns>
        public async Task<RecipeIngredient?> GetRecipeIngredientByIdAsync(Guid id)
        {
            return await _unitOfWork.RecipeIngredientRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据食谱ID获取食谱食材列表
        /// </summary>
        /// <param name="recipeId">食谱ID</param>
        /// <returns>食谱食材列表</returns>
        public async Task<IEnumerable<RecipeIngredient>> GetRecipeIngredientsByRecipeIdAsync(Guid recipeId)
        {
            return await _unitOfWork.RecipeIngredientRepository.GetByRecipeIdAsync(recipeId);
        }

        /// <summary>
        /// 创建食谱食材
        /// </summary>
        /// <param name="recipeId">食谱ID</param>
        /// <param name="name">名称</param>
        /// <param name="amount">数量</param>
        /// <param name="unit">单位</param>
        /// <returns>食谱食材实体</returns>
        public async Task<RecipeIngredient> CreateRecipeIngredientAsync(Guid recipeId, string name, decimal amount, string unit)
        {
            var recipeIngredient = RecipeIngredient.Create(recipeId, name, amount, unit);
            await _unitOfWork.RecipeIngredientRepository.AddAsync(recipeIngredient);
            await _unitOfWork.SaveChangesAsync();
            return recipeIngredient;
        }

        /// <summary>
        /// 更新食谱食材
        /// </summary>
        /// <param name="id">食谱食材ID</param>
        /// <param name="name">名称</param>
        /// <param name="amount">数量</param>
        /// <param name="unit">单位</param>
        /// <returns>食谱食材实体</returns>
        public async Task<RecipeIngredient?> UpdateRecipeIngredientAsync(Guid id, string? name, decimal? amount, string? unit)
        {
            var recipeIngredient = await _unitOfWork.RecipeIngredientRepository.GetByIdAsync(id);
            if (recipeIngredient == null)
            {
                return null;
            }

            recipeIngredient.Update(name, amount, unit);
            await _unitOfWork.RecipeIngredientRepository.UpdateAsync(recipeIngredient);
            await _unitOfWork.SaveChangesAsync();
            return recipeIngredient;
        }

        /// <summary>
        /// 删除食谱食材
        /// </summary>
        /// <param name="id">食谱食材ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteRecipeIngredientAsync(Guid id)
        {
            var recipeIngredient = await _unitOfWork.RecipeIngredientRepository.GetByIdAsync(id);
            if (recipeIngredient == null)
            {
                return false;
            }

            await _unitOfWork.RecipeIngredientRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }

    /// <summary>
    /// 食谱步骤服务实现
    /// </summary>
    public class RecipeStepService : IRecipeStepService
    {
        private readonly IRecipeStepRepository _recipeStepRepository;
        private readonly IUnitOfWork _unitOfWork;

        public RecipeStepService(IRecipeStepRepository recipeStepRepository, IUnitOfWork unitOfWork)
        {
            _recipeStepRepository = recipeStepRepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取食谱步骤
        /// </summary>
        /// <param name="id">食谱步骤ID</param>
        /// <returns>食谱步骤实体</returns>
        public async Task<RecipeStep?> GetRecipeStepByIdAsync(Guid id)
        {
            return await _recipeStepRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据食谱ID获取食谱步骤列表
        /// </summary>
        /// <param name="recipeId">食谱ID</param>
        /// <returns>食谱步骤列表</returns>
        public async Task<IEnumerable<RecipeStep>> GetRecipeStepsByRecipeIdAsync(Guid recipeId)
        {
            return await _recipeStepRepository.GetByRecipeIdAsync(recipeId);
        }

        /// <summary>
        /// 创建食谱步骤
        /// </summary>
        /// <param name="recipeId">食谱ID</param>
        /// <param name="stepNumber">步骤编号</param>
        /// <param name="description">描述</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>食谱步骤实体</returns>
        public async Task<RecipeStep> CreateRecipeStepAsync(Guid recipeId, int stepNumber, string description, string? imageUrl = null)
        {
            var recipeStep = RecipeStep.Create(recipeId, stepNumber, description, imageUrl);
            await _recipeStepRepository.AddAsync(recipeStep);
            await _unitOfWork.SaveChangesAsync();
            return recipeStep;
        }

        /// <summary>
        /// 更新食谱步骤
        /// </summary>
        /// <param name="id">食谱步骤ID</param>
        /// <param name="stepNumber">步骤编号</param>
        /// <param name="description">描述</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>食谱步骤实体</returns>
        public async Task<RecipeStep?> UpdateRecipeStepAsync(Guid id, int? stepNumber, string? description, string? imageUrl = null)
        {
            var recipeStep = await _recipeStepRepository.GetByIdAsync(id);
            if (recipeStep == null)
            {
                return null;
            }

            recipeStep.Update(stepNumber, description, imageUrl);
            await _recipeStepRepository.UpdateAsync(recipeStep);
            await _unitOfWork.SaveChangesAsync();
            return recipeStep;
        }

        /// <summary>
        /// 删除食谱步骤
        /// </summary>
        /// <param name="id">食谱步骤ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteRecipeStepAsync(Guid id)
        {
            var recipeStep = await _recipeStepRepository.GetByIdAsync(id);
            if (recipeStep == null)
            {
                return false;
            }

            await _recipeStepRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }

    /// <summary>
    /// 食谱营养服务实现
    /// </summary>
    public class RecipeNutrientService : IRecipeNutrientService
    {
        private readonly IRecipeNutrientRepository _recipeNutrientRepository;
        private readonly IUnitOfWork _unitOfWork;

        public RecipeNutrientService(IRecipeNutrientRepository recipeNutrientRepository, IUnitOfWork unitOfWork)
        {
            _recipeNutrientRepository = recipeNutrientRepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取食谱营养
        /// </summary>
        /// <param name="id">食谱营养ID</param>
        /// <returns>食谱营养实体</returns>
        public async Task<RecipeNutrient?> GetRecipeNutrientByIdAsync(Guid id)
        {
            return await _recipeNutrientRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据食谱ID获取食谱营养列表
        /// </summary>
        /// <param name="recipeId">食谱ID</param>
        /// <returns>食谱营养列表</returns>
        public async Task<IEnumerable<RecipeNutrient>> GetRecipeNutrientsByRecipeIdAsync(Guid recipeId)
        {
            return await _recipeNutrientRepository.GetByRecipeIdAsync(recipeId);
        }

        /// <summary>
        /// 创建食谱营养
        /// </summary>
        /// <param name="recipeId">食谱ID</param>
        /// <param name="nutrientName">营养名称</param>
        /// <param name="amount">数量</param>
        /// <param name="unit">单位</param>
        /// <returns>食谱营养实体</returns>
        public async Task<RecipeNutrient> CreateRecipeNutrientAsync(Guid recipeId, string nutrientName, decimal amount, string unit)
        {
            var recipeNutrient = RecipeNutrient.Create(recipeId, nutrientName, amount, unit);
            await _recipeNutrientRepository.AddAsync(recipeNutrient);
            await _unitOfWork.SaveChangesAsync();
            return recipeNutrient;
        }

        /// <summary>
        /// 更新食谱营养
        /// </summary>
        /// <param name="id">食谱营养ID</param>
        /// <param name="nutrientName">营养名称</param>
        /// <param name="amount">数量</param>
        /// <param name="unit">单位</param>
        /// <returns>食谱营养实体</returns>
        public async Task<RecipeNutrient?> UpdateRecipeNutrientAsync(Guid id, string? nutrientName, decimal? amount, string? unit)
        {
            var recipeNutrient = await _recipeNutrientRepository.GetByIdAsync(id);
            if (recipeNutrient == null)
            {
                return null;
            }

            recipeNutrient.Update(nutrientName, amount, unit);
            await _recipeNutrientRepository.UpdateAsync(recipeNutrient);
            await _unitOfWork.SaveChangesAsync();
            return recipeNutrient;
        }

        /// <summary>
        /// 删除食谱营养
        /// </summary>
        /// <param name="id">食谱营养ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteRecipeNutrientAsync(Guid id)
        {
            var recipeNutrient = await _recipeNutrientRepository.GetByIdAsync(id);
            if (recipeNutrient == null)
            {
                return false;
            }

            await _recipeNutrientRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }

    /// <summary>
    /// 用户食谱服务实现
    /// </summary>
    public class UserRecipeService : IUserRecipeService
    {
        private readonly IUserRecipeRepository _userRecipeRepository;
        private readonly IUnitOfWork _unitOfWork;

        public UserRecipeService(IUserRecipeRepository userRecipeRepository, IUnitOfWork unitOfWork)
        {
            _userRecipeRepository = userRecipeRepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取用户食谱
        /// </summary>
        /// <param name="id">用户食谱ID</param>
        /// <returns>用户食谱实体</returns>
        public async Task<UserRecipe?> GetUserRecipeByIdAsync(Guid id)
        {
            return await _userRecipeRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据用户ID获取用户食谱列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>用户食谱列表</returns>
        public async Task<IEnumerable<UserRecipe>> GetUserRecipesByUserIdAsync(Guid userId)
        {
            return await _userRecipeRepository.GetByUserIdAsync(userId);
        }

        /// <summary>
        /// 根据用户ID和食谱ID获取用户食谱列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="recipeId">食谱ID</param>
        /// <returns>用户食谱列表</returns>
        public async Task<IEnumerable<UserRecipe>> GetUserRecipesByUserIdAndRecipeIdAsync(Guid userId, Guid recipeId)
        {
            return await _userRecipeRepository.GetByUserIdAndRecipeIdAsync(userId, recipeId);
        }

        /// <summary>
        /// 创建用户食谱
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="recipeId">食谱ID</param>
        /// <param name="notes">备注</param>
        /// <returns>用户食谱实体</returns>
        public async Task<UserRecipe> CreateUserRecipeAsync(Guid userId, Guid recipeId, string? notes = null)
        {
            var userRecipe = UserRecipe.Create(userId, recipeId, notes);
            await _userRecipeRepository.AddAsync(userRecipe);
            await _unitOfWork.SaveChangesAsync();
            return userRecipe;
        }

        /// <summary>
        /// 更新用户食谱
        /// </summary>
        /// <param name="id">用户食谱ID</param>
        /// <param name="notes">备注</param>
        /// <returns>用户食谱实体</returns>
        public async Task<UserRecipe?> UpdateUserRecipeAsync(Guid id, string? notes)
        {
            var userRecipe = await _userRecipeRepository.GetByIdAsync(id);
            if (userRecipe == null)
            {
                return null;
            }

            userRecipe.Update(notes);
            await _userRecipeRepository.UpdateAsync(userRecipe);
            await _unitOfWork.SaveChangesAsync();
            return userRecipe;
        }

        /// <summary>
        /// 删除用户食谱
        /// </summary>
        /// <param name="id">用户食谱ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteUserRecipeAsync(Guid id)
        {
            var userRecipe = await _userRecipeRepository.GetByIdAsync(id);
            if (userRecipe == null)
            {
                return false;
            }

            await _userRecipeRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 检查用户是否收藏了食谱
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="recipeId">食谱ID</param>
        /// <returns>是否收藏</returns>
        public async Task<bool> IsUserFavoriteRecipeAsync(Guid userId, Guid recipeId)
        {
            return await _userRecipeRepository.IsUserFavoriteRecipeAsync(userId, recipeId);
        }
    }
}